//#########################################################################
//                              WackiEngie
//           Copyright (c) 2014 Marc Wacker. All rights reserved.
//#########################################################################

#include "WeBaseApplication.h"
#include "WePlatform.h"
#include "WeInput.h"
#include "WeXInputDeviceHandler.h"
#include "WeTime.h"
#include "WeDebug.h"

namespace WackiEngine
{

	BaseApplication::BaseApplication(int32 width, int32 height, const String& title)
	{
		// initialize all necessary core modules
		Platform::startUp(width, height, title); // atm window setup is here...
		Time::startUp();
		Input::startUp();

		// add xbox controller
		gInput().registerInputDeviceHandler(std::shared_ptr<InputDeviceHandler>(new XInputDH));

		gPlatform().windowClosed.connect(this, &BaseApplication::onWindowClose);
	}

	BaseApplication::~BaseApplication()
	{
		// shut down our modules

		Input::shutDown();
		Time::shutDown();

		// platform last
		Platform::shutDown();
	}

	void BaseApplication::run()
	{
		_running = true;

		while(_running)
		{
			// update all modules
			gPlatform().update();
			gTime().update();
			gInput().update();	// maybe we should protect the input.update function? this should only be called once per frame, and that is here...

			// application update hook
			update();
			
			gPlatform().swapBuffers();

		}

	}

	void BaseApplication::stop()
	{
		_running = false;
	}

	void BaseApplication::onWindowClose()
	{
		stop();
	}

}